package com.yanqu.road.dao.impl.log;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.log.LogVarietyDispatch;
import com.yanqu.road.entity.log.LogVarietyRank;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ：zfr
 * @date ：2021/3/23 18:19
 */
public class LogVarietyDaoImpl extends TempDao {

    public boolean addLogVarietyDispatchList(List<LogVarietyDispatch> list) {
        boolean result = false;
        getDb().setConnName(DbNameStrategy.getLogDb());
        String sql = "INSERT INTO t_l_variety_dispatch(userId,type,level,patronsCount,patronsList,patronsEarnSpeed,dispatchTime) " +
                "VALUES(?,?,?,?,?,?,?)";
        List<Map<Integer, DbParameter>> mapList = new ArrayList<>();
        for (LogVarietyDispatch data : list) {
            Map<Integer, DbParameter> para = new HashMap<>();
            int i = 1;
            para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
            para.put(i++, new DbParameter(Types.INTEGER, data.getType()));
            para.put(i++, new DbParameter(Types.INTEGER, data.getLevel()));
            para.put(i++, new DbParameter(Types.INTEGER, data.getPatronsCount()));
            para.put(i++, new DbParameter(Types.VARCHAR, StringUtils.listToString(data.getPatronsList(), ",")));
            para.put(i++, new DbParameter(Types.VARCHAR, data.getPatronsEarnSpeed().toString()));
            para.put(i++, new DbParameter(Types.TIMESTAMP, data.getDispatchTime()));
            mapList.add(para);
        }
        if (getDb().executeBatchQuery(sql, mapList) > -1) {
            result = true;
        }
        return result;
    }

    public boolean addLogVarietyRankList(List<LogVarietyRank> list) {
        boolean result = false;
        getDb().setConnName(DbNameStrategy.getLogDb());
        String sql = "INSERT INTO t_l_variety_rank(userId,type,level,rank,dispatchTime" +
                "VALUES(?,?,?,?,?)";
        List<Map<Integer, DbParameter>> mapList = new ArrayList<>();
        for (LogVarietyRank data : list) {
            Map<Integer, DbParameter> para = new HashMap<>();
            int i = 1;
            para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
            para.put(i++, new DbParameter(Types.INTEGER, data.getType()));
            para.put(i++, new DbParameter(Types.INTEGER, data.getLevel()));
            para.put(i++, new DbParameter(Types.INTEGER, data.getRank()));
            para.put(i++, new DbParameter(Types.INTEGER, data.getDispatchTime()));
            mapList.add(para);
        }
        if (getDb().executeBatchQuery(sql, mapList) > -1) {
            result = true;
        }
        return result;
    }
}
